package com.atguigu.algorithm;

import java.util.Arrays;

public class BinarySearch {
    public static void main(String[] args) {
        int[] a=new int[]{9, 65, 51, 31, 72, 25, 38, 55, 6, 16};
        Arrays.sort(a);
        System.out.println(binarySearch(a, 6, 0, a.length));

    }
    public static int binarySearch(int[] a,int key,int left,int right){
        //假设该有序数组为升序排序
        int mid=left+(right-left)>>1;
        if(left>=right){
            return -1;
        }
        if(a[mid]==key){
            return mid;
        }else if(a[mid]>key){
            return binarySearch(a,key,left,mid-1);
        }else{
            return binarySearch(a,key,mid+1,right);
        }
    }
}
